#include "swpprocessdocument.h"

#include "../editors/items/graphicitemcommon.h"
#include "../utils/processdocumentautocomplete.h"

#include "swpprojectdocument.h"

#include <coremimetypes.h>
#include <gdmodelquerymaker.h>
#include <icontainerdocument.h>
#include <ieditoritem.h>
#include <mimetypemanager.h>

#include <QtGui/QApplication>
#include <QtGui/QDesktopWidget>

using namespace core;
using namespace swp;

typedef QSharedPointer<core::IDocument> Document;

SwpProcessDocument::SwpProcessDocument(const QString& name,
    const MimeType& mimeType,
    const QSharedPointer<core::IContainerDocument>& container,
    bool isFuture) : SwpAbstractLinkedDocument(name, mimeType, container,
        isFuture),
        m_graphicData(new GraphicData())
{
    const int width = QApplication::desktop()->width();
    const int height = QApplication::desktop()->height();

    m_graphicData->setSceneRect(0, 0, width, height);

    connect(m_graphicData, SIGNAL(dataChanged()),
        this, SLOT(makeDirty()));

    m_graphicData->addSnapToGridException(SWP_SWIMLANE_ITEM);

    m_data = m_graphicData;
}

SwpProcessDocument::~SwpProcessDocument()
{

}

QList<MimeType> SwpProcessDocument::linksTo() const
{
    QList<MimeType> ret;

    ret.append(MimeTypeManager::getInstance()->mimeType(
        CLASS_DIAGRAM_MIMETYPE));

    return ret;
}

void SwpProcessDocument::linksUpdated(bool projectIsOpening)
{
    ProcessDocumentAutocomplete* autocomplete =
        ProcessDocumentAutocomplete::getInstance();

    QList<QString> autocompleteData;

    foreach(const Document& link, m_links)
    {
        GraphicData* data = dynamic_cast<GraphicData* >(link->data());

        Q_ASSERT(data != 0); // shouldn't happen

        GDModelQueryMaker queryMaker(*data);

        const QList<IGraphicEditorItem*> workers =
            queryMaker.getItems(SWP_WORKER_ITEM);

        foreach (IGraphicEditorItem* worker, workers)
        {
            autocompleteData.append(
                worker->propertyValue("class").toString());
        }

        autocomplete->refreshSwimlanes(m_graphicData, autocompleteData);

        autocompleteData.clear();

        const QList<IGraphicEditorItem*> resources =
            queryMaker.getItems(SWP_RESOURCE_ITEM);

        const QList<IGraphicEditorItem*> artifacts =
            queryMaker.getItems(SWP_ARTIFACT_ITEM);

        foreach (IGraphicEditorItem* resource, resources)
        {
            autocompleteData.append(
                resource->propertyValue("class").toString());
        }

        foreach (IGraphicEditorItem* artifact, artifacts)
        {
            autocompleteData.append(
                artifact->propertyValue("class").toString());
        }

        autocomplete->refreshInputs(m_graphicData, autocompleteData);

        autocompleteData.clear();        

        foreach (IGraphicEditorItem* artifact, artifacts)
        {
            autocompleteData.append(
                artifact->propertyValue("class").toString());
        }

        autocomplete->refreshOutputs(m_graphicData, autocompleteData);
    }

    if(!projectIsOpening)
    {
        QSharedPointer<SwpProjectDocument> project =
            qSharedPointerDynamicCast<SwpProjectDocument>(container());

        if (!project.isNull())
        {
            project->linksUpdated();
        }
    }
}
